package com.cy.pj.notice.web.controller;

import com.cy.pj.common.util.PageUtil;
import com.cy.pj.notice.pojo.SysNotice;
import com.cy.pj.notice.service.SysNoticeService;
import com.cy.pj.notice.web.pojo.JsonResult;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/notice/")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;

    /**
     * 处理客户端的查询请求
     * 不带参数的请求url：http://localhost/notice/
     * 带参数的请求url: http://localhost/notice/?type=1&title=my
     */
    @GetMapping
    public JsonResult doFindNotices(SysNotice sysNotice) {
        /*return new JsonResult(PageHelper.startPage(1,3)
        .doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                sysNoticeService.findNotices(sysNotice);
            }
        }));*/
        /*return new JsonResult(PageUtil.startPage().doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                sysNoticeService.findNotices(sysNotice);
            }
        }));*/
        return new JsonResult(PageUtil.startPage().doSelectPageInfo(() -> sysNoticeService.findNotices(sysNotice)));
        //return new JsonResult(sysNoticeService.findNotices(sysNotice));
        //这个结果会返给谁？调用方(DispatcherServlet)
        //调用方拿到方法执行结果以后会调用相关api将结果转换为json格式字符串
        //最后通过响应对象将字符串响应到客户端
    }

    @PostMapping
    public JsonResult doSaveNotices(SysNotice sysNotice) {
        sysNoticeService.saveNotices(sysNotice);
        return new JsonResult("save ok");
    }

    @PutMapping
    public JsonResult doUpdateNotices(@RequestBody SysNotice sysNotice) {
        System.out.println("sysnotice" + sysNotice);
        sysNoticeService.updateNotice(sysNotice);
        return new JsonResult("update ok");
    }

    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable Long id) {
        return new JsonResult(sysNoticeService.findById(id));
    }

    @DeleteMapping("{ids}")
    public JsonResult doDeleteById(@PathVariable("ids") Long id) {
        sysNoticeService.deleteById(id);
        return new JsonResult("delete ok");
    }

}
